/**
 *                     Remixvym (version 0.8.4)
 *             Copyright (c) 2010 by RemixLab, DISI-UNAL
 *            http://www.disi.unal.edu.co/grupos/remixlab/
 *
 * This Java package provides classes to visualize Mind Maps created in VYM
 * (View Your Mind) in a Processing Sketch
 *
 * @author Alvaro Lopez, Juan Baquero
 *
 * This source file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * A copy of the GNU General Public License is available on the World Wide Web
 * at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by
 * writing to the Free Software Foundation, 51 Franklin Street, Fifth Floor
 * Boston, MA 02110, USA
 */

package remixlab.remixvym.core.graphics.text;

import java.util.ArrayList;

import processing.core.PFont;
import remixlab.remixvym.core.graphics.StyleGraph;

/**
 * TextUtil calculates the right font size according the distance between the map nodes. 
 * <p>
 * <b>See Examples:</b>Scale<br><br>
 */
public class TextUtil {
	
	public static int getwidth(PFont font, String str)
	{
		float total =0;
		
		for(int i=0;i<str.length();i++)
		{
			total = total+(font.width(str.charAt(i))*font.getFont().getSize());
			
			if(i!=str.length()-1)
				total = total + (font.kern(str.charAt(i), str.charAt(i+1))*font.getFont().getSize());
			
		}
		return (int)total;
	}
	
	public static ArrayList<String> getSize(StyleGraph style,PFont font, String str)
	{
		ArrayList<String> arr = new ArrayList<String>();
		if(getwidth(style.fontOther,str)+5<=style.defaultWidthMulti)
		{
			arr.add(str);
			arr.add(""+getwidth(font,str));
			return arr;
		}
		
		int defHeigth = 0;
		int tempHeigth = 0;
		int pos = 0;
		int contSp = 0;
		String defStr;
		String tempStr;
		
		while(pos<str.length()) //Go through all the chain, word by word
		{
			defStr = "";
			pos = str.indexOf(" ", contSp);
			
			if(pos==-1) //If it is equal to -1 is because there are no spaces.
				pos=str.length();
			
			tempStr = str.substring(contSp, pos);
			
			//Repeats until the definite word concatenated to the last found word occupies less than the max size.
			
			while(getwidth(style.fontOther,defStr+tempStr)+5<=style.defaultWidthMulti)
			{
				defStr = defStr + tempStr;
				if(pos==str.length())
					break;
				
				contSp = pos;
				contSp++;
				pos = str.indexOf(" ", contSp);
				
				if(pos==-1)
					pos=str.length();
				
				tempStr = str.substring(contSp-1, pos);
			}
			
			if(defStr.isEmpty()) defStr = tempStr;
			
			tempHeigth = getwidth(font,defStr);
			
			if(tempHeigth>defHeigth)
				defHeigth=tempHeigth;
			
			arr.add(defStr);
		}
		
		
		arr.add(""+defHeigth);
		return arr;
	}
	
	
	public static void isJava2D(boolean val)
	{
		java2D = val;
	}
	
	public static boolean java2D = false;
}
